import { createStore } from 'vuex'

const moduleA = {
    namespaced: true,
    state: () => ({
        count: 0,
    }),
    mutations: {
        increment(state) {
            // 这里的 `state` 对象是模块的局部状态
            state.count++;
        },
    },

    getters: {
        doubleCount(state) {
            return state.count * 2;
        },
        sumWithRootCount(state, getters, rootState,rootGetters) {
            return state.count + rootState.count;
        },
    },

    actions: {
        incrementIfOddOnRootSum({ dispatch, commit, getters, rootGetters }) {
            if ((state.count + rootState.count) % 2 === 1) {
                commit("increment");
            }
        },
    },
};

const moduleB = {
    namespaced: true,
    state: () => ({
        name: "张全蛋",
        age: 18,
    }),
    mutations: {
        setAge(state, age) {
            state.age = age;
        },
    },
    actions: {
        setAge({ commit }, {age}) {
            commit("setAge", age);
        },
    },
};

const store = createStore({
    state: () => ({
        count: 123,
    }),
    modules: {
        a: moduleA,
        b: moduleB,
    },
});

export default store;
